using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MapPanel;

namespace CityLamp
{
    public partial class SwitchForm : Form
    {
        private MainForm parentForm = null;
        public SwitchForm(MainForm form)
        {
            InitializeComponent();
            parentForm = form;
            parentForm.OnLampWarningInfoChanged += new MainForm.lampWarningInfoChangedEventHandler(switchForm_OnLampWarningInfoChanged);
        }

        public void switchForm_OnLampWarningInfoChanged(object sender, MapPanel.LampWarningArgs e)
        {
            if (e.Command == mapControl.CLCOMMAND.SETOUTPUTSTATE)
            {
                this.nodeListControl1.SetResultAsSuccess(e.NodeId);                
            }
        }

        private void SwitchForm_Load(object sender, EventArgs e)
        {
            this.nodeListControl1.StreetLampList = parentForm.GetStreetLampList();
            this.nodeListControl1.UpdateNodeListView();
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            this.nodeListControl1.ClearNodeListViewResult();

            byte status = this.GetOutputStatus();

            List<MapPanel.StreetLamp> lampListSelected = this.nodeListControl1.GetSelectedLampList();

            if (lampListSelected.Count > 0)
            {
                foreach (MapPanel.StreetLamp streetLamp in lampListSelected)
                {
                    this.parentForm.SetControllerOutputStatus(streetLamp, status);
                }
            }
        }

        private byte GetOutputStatus()
        {
            byte status = 0x00;
            if (this.checkBox1.Checked)
                status |= 0x01;
            if (this.checkBox2.Checked)
                status |= 0x02;
            if (this.checkBox3.Checked)
                status |= 0x04;
            if (this.checkBox4.Checked)
                status |= 0x08;
            if (this.checkBox5.Checked)
                status |= 0x10;
            if (this.checkBox6.Checked)
                status |= 0x20;
            if (this.checkBox7.Checked)
                status |= 0x40;
            if (this.checkBox8.Checked)
                status |= 0x80;

            return status;
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SwitchForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            parentForm.OnLampWarningInfoChanged -= switchForm_OnLampWarningInfoChanged;
        }
    }
}
